function [Z, P, gain, order] = filter_cauer (filterType, aa, ap, wa, wp, waMENOS, waMAS, wpMENOS, wpMAS, order, sliderK)
if order==0
    forced=1;
    order=1;
else forced=0;
end
% ripple= 1-1/sqrt(10^(ap/10));
cumple=0;

while ~cumple
% aa2=aa-ap;
switch filterType
    case 1
    w=[wpMENOS wpMAS];
    sample=wpMENOS;
    [Z,P,k]=ellip(order,ap,aa,w,'s');
    case 2
    [Z,P,k]=ellip(order,ap,aa,wp,'s');
    case 3
    [Z,P,k]=ellip(order,ap,aa,wp,'high','s');
    case 4
    w=[wpMENOS wpMAS];
    [Z,P,k]=ellip(order,ap,aa,w,'stop','s');
end
Z=Z';
P=P';
% gain=k*(10^((-ap+ripple)/20));
gain=k;

if forced==1
H=gain*tf(poly(Z),poly(P));
switch filterType
    case 1
        [b]=bode(H, waMENOS);
        b=-20*log10(b);
        if (b>aa)
            cumple=1;
        else order=order+1;
        end
        
    case 2
        [b]=bode(H, wa);
        b=-20*log10(b);
        if (b>aa)
            cumple=1;
        else order=order+1;   
        end
    case 3
        [b]=bode(H, wa);
        b=-20*log10(b);
        if (b>aa)
            cumple=1;
        else order=order+1;
        end
    case 4
        [b]=bode(H, waMENOS);
        b=-20*log10(b);
        if (b>aa)
            cumple=1;
        else order=order+1;
        end
end
else cumple=1;
end
if sliderK~=0 && cumple==1;
H=gain*tf(poly(Z),poly(P));
switch filterType
    case 2
        sample=logspace(log10(wp),log10(10*wa),5000);
        a=bode(H,sample);
        a=-20*log10(a);
        for j=1:length(sample)
            if a(j)>aa
                break;
            end
        end
        correction=sliderK*(wa-sample(j))*wp/sample(j);
    case 3
        sample=logspace(log10(wa/10),log10(wp),5000);
        a=bode(H,sample);
        a=-20*log10(a);
        for j=length(sample):-1:1
            if a(j)>aa
                break;
            end
        end
        correction=sliderK*(wa-sample(j))*wp/sample(j);
    case 1
        sample=logspace(log10(waMENOS/10),log10(wpMENOS),10000);
        a=bode(H,sample);
        a=-20*log10(a);
        for j=length(sample):-1:1
            if a(j)>aa
                break;
            end
        end
        if forced==1
            correction=-sliderK*(waMENOS-sample(j))*wpMENOS/(sample(j)*2);
        else
            correction=-sliderK*(waMENOS-sample(j))*wpMENOS/sample(j);   
        end
    case 4
        sample=logspace(log10(wpMENOS),log10(10*waMENOS),10000);
        a=bode(H,sample);
        a=-20*log10(a);
        for j=1:length(sample)
            if a(j)>aa
                break;
            end
        end
        correction=-sliderK*(waMENOS-sample(j))*wpMENOS/sample(j);
end
    [Z, P, gain, order] = filter_cauer (filterType, aa, ap, wa, wp+correction, waMENOS, waMAS, wpMENOS-correction, wpMAS*wpMENOS/(wpMENOS-correction), order, 0);

end
end

end